<?php

class Admin_UserController extends Zend_Controller_Action
{
	private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction(){
        $modelRole  = new Admin_Model_Role();
        $modelUser  = new Admin_Model_Admin();
        $userList   = $modelUser->getAllData()->toArray();
        foreach($userList as $key => $row){
            $userList[$key]['roleList']   = $modelRole->getDataByUserId($row['adminId'])->toArray();
        }
        $this->view->userList = $userList;
        $this->view->userCheck = $modelUser->getDataById($this->_auth['adminId']);
    }

    
    public function deleteAction(){
        $adminId = $this->_getParam('id', 0);
        $modelUser = new Admin_Model_Admin();
        $userCheck = $modelUser->getDataById($adminId);
        if($userCheck){
            $this->view->userCheck = $userCheck;
            if($this->getRequest()->isPost()){
                $del = $this->getRequest()->getPost('del');
                if($del == 'Có'){
                    $adminId = $this->getRequest()->getPost('adminId');
                    $modelUser->deleteData($adminId);
                    $modelUserrole = new Admin_Model_Userrole();
                    $modelUserrole->deleteData($adminId);
                   
                }
                $this->_helper->redirector('index', 'user', 'admin');
            }else{
                
                if($this->_auth['adminId'] == $adminId){
                    $this->_helper->redirector('index', 'user', 'admin');
                    exit();
                }
            }
        }
    }
    public function addAction(){
        $form = new Admin_Form_User();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $username   = $form->getValue('username');
                $email      = $form->getValue('email');
                $password   = $form->getValue('password');
                $userRole   = $form->getValue('userRole');
                $status     = $form->getValue('status');
                
                $modelUser = new Admin_Model_Admin();
				if(strlen($password) < 6){
					$this->view->mess = "Mật khẩu tối thiểu 6 ký tự!!!";
                    $this->view->flag = "alert";
				}elseif($modelUser->getDataByUsername($username)){
					$this->view->mess = "Username đã tồn tại!!!";
                    $this->view->flag = "alert";
				}else{
					$adminId = $modelUser->addData($username, $password, $status, $email);
                    if($adminId){
                        //Thêm các quyền của user
                        $count = count($userRole);
                    	$dataItem = array();
                    	for($i=0;$i<$count;++$i){
                    		if(!$userRole[$i]){
                    			continue;
                    		}
                    		$dataItem[] = '('.$adminId . ',' . (int)($userRole[$i]).')';
                    	}
                        $modelUserrole = new Admin_Model_Userrole();
                		$modelUserrole->addMultiData($dataItem);
                        
                    }
                    $this->_helper->redirector('index', 'user', 'admin');
				}

            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction(){
		$form = new Admin_Form_User();
		$form->submit->setLabel('Lưu thay đổi');
		$this->view->form = $form;

		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)){

				$dataUpdate['adminId'] = (int)($form->getValue('adminId'));
				$dataUpdate['username'] = $form->getValue('username');
                $dataUpdate['email']      = $form->getValue('email');
				$dataUpdate['password'] = $form->getValue('password');
				$dataUpdate['userRole'] = $form->getValue('userRole');
                $dataUpdate['status']     = $form->getValue('status');
               
                $modelUser = new Admin_Model_Admin();
                $userCheck = $modelUser->getDataByUsername($dataUpdate['username']);
                if(!$userCheck || $userCheck['adminId'] == $dataUpdate['adminId']){
                    $check = 1;
				    if($dataUpdate['password']){
				        if(strlen($dataUpdate['password']) < 6){
				            $this->view->mess = "Mật khẩu tối thiểu 6 ký tự!!!";
                            $this->view->flag = "alert";
                            $check = 0;
                        }
    				}
                    if($check){
        				$modelUser->updateData($dataUpdate['adminId'], $dataUpdate['username'], $dataUpdate['password'], $dataUpdate['status'], $dataUpdate['email']);
                        
                        $modelUserrole = new Admin_Model_Userrole();
                        $modelUserrole->deleteData($dataUpdate['adminId']);
                
                		$dataRole = array();
                		if(count($dataUpdate['userRole']) > 0){
                			for($i = 0; $i<count($dataUpdate['userRole']); $i++){
                				$dataRole[] =	'('. $dataUpdate['adminId'] . ',' . $dataUpdate['userRole'][$i] .')';
                			}
                			$dataRole = array_unique($dataRole);
                
                            $modelUserrole->addMultiData($dataRole);
                		}
                        
                        $this->_helper->redirector('index', 'user', 'admin');
                    }else{
                        $form->populate($formData);
                    }
                }else{
                    $this->view->mess ="Username đã tồn tại!!!";
                    $this->view->flag = "alert";
    				$form->populate($formData);
    			}

			}else{
				$form->populate($formData);
			}
		}else{
			$dataUpdate['adminId'] = (int)($this->_getParam('id', 0));
                
            $modelUser = new Admin_Model_Admin();
			$userCheck = $modelUser->getDataById($dataUpdate['adminId']);
            if($userCheck){
                $modelRole = new Admin_Model_Role();
                $roleList = $modelRole->getDataByUserId($dataUpdate['adminId'])->toArray();
               
    			$this->view->roleList = $roleList;
    			$this->view->userCheck = $userCheck;
    			$form->populate($userCheck->toArray());
            }else{
                $this->_helper->redirector('index', 'user', 'admin');
            }
		}
    }
}





